home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / CALCPAD.AML < prev    next >
Text File  |  1996-07-17  |  7KB  |  338 lines

  1. //--------------------------------------------------------------------
  2. // CALCPAD.AML
  3. // Calculator Pad, (C) 1993-1996 by nuText Systems
  4. //
  5. // (See Calcpad.dox for user help)
  6. //
  7. // This macro displays a simple calculator pad with basic arithmetic
  8. // operations, a memory, and four display lines. Floating point numbers
  9. // are not supported.
  10. //
  11. // Usage:
  12. //
  13. // Select this macro from the Macro List (on the Macro menu), or run it
  14. // from the macro picklist <shift f12>.
  15. //--------------------------------------------------------------------
  16.  
  17. include bootpath "define.aml"
  18.  
  19. // define the colors to use
  20. constant  calc_display_color       = color darkgray  on green
  21. constant  calc_display_curr_color  = color black     on green
  22. constant  calc_button_color        = color black     on green
  23. constant  calc_button_hilite_color = color black     on brightgreen
  24. constant  calc_client_color        = color black     on gray
  25. constant  calc_border_color        = color white     on gray
  26. constant  calc_control_color       = color yellow
  27.  
  28. // keep this object resident
  29. resident ON
  30. settype "win"
  31.  
  32. // display lines
  33. variable line1, line2, line3, line4
  34.  
  35. // line number of last lbutton click
  36. variable line, bcol
  37.  
  38. // display operators
  39. variable op0, op1, op2, op3, op4
  40.  
  41. // numeric base
  42. radix = 10
  43.  
  44. // clear the calculator display
  45. private function cleardisp
  46.   line1 = ''
  47.   line2 = ''
  48.   line3 = ''
  49.   line4 = 0
  50.   op0 = ' '
  51.   op1 = ' '
  52.   op2 = ' '
  53.   op3 = ' '
  54.   op4 = ' '
  55. end
  56.  
  57. // scroll the display upwards
  58. private function scrollup
  59.   op0   = op1
  60.   line1 = line2
  61.   op1   = op2
  62.   line2 = line3
  63.   op2   = op3
  64.   line3 = line4
  65.   op3   = op4
  66.   line4 = 0
  67.   op4   = ' '
  68. end
  69.  
  70. // perform the operation currently on the display
  71. private function result
  72.   value = case op3
  73.             when '+'  line3 + line4
  74.             when '-'  line3 - line4
  75.             when '*'  line3 * line4
  76.             when '/'  line3 / line4
  77.             when 'm'  line3 mod line4
  78.             when '%'  (line3 * line4) / 100
  79.           end
  80.   scrollup
  81.   line4 = value
  82. end
  83.  
  84.  
  85. // create the calculator window
  86. createwindow
  87. setframe ">b"
  88. setcolor  border_color    calc_border_color
  89. setcolor  text_color      calc_client_color
  90. setcolor  control_color   calc_control_color
  91. setcolor  border_flash_color   (color brightgreen on gray)
  92. settitle "Calculator"
  93. setwinctrl '≡'
  94.  
  95. width = 36
  96. height = 15
  97.  
  98. // center the window
  99. ox = (getvidcols - width) / 2
  100. oy = (getvidrows - height) / 2
  101. sizewindow ox oy ox + width oy + height "ad"
  102.  
  103. setborder "1i"
  104. setshadow 2 1
  105.  
  106. // define strings to display in the keypad
  107. keystr = "  C        %   mod   /    CE   7    8    9    *    MR   4    5    6    -    MS   1    2    3    +    M+   0       +/-   =  "
  108.  
  109. // draw the keypad
  110. gotoxy 3 7
  111. for y = 0 to 4 do
  112.   for x = 0 to 4 do
  113.     writestr keystr [y * 25 + (x * 5) + 1 : 5]  calc_button_color
  114.     writestr "  "
  115.   end
  116.   if y <> 4 then
  117.     writeline
  118.     writeline
  119.     gotoxy 3
  120.   end
  121. end
  122.  
  123. x = 0
  124. y = 0
  125. memory = ''
  126.  
  127. // clear the display
  128. cleardisp
  129.  
  130. private function draw
  131.   // draw the calculator display
  132.   gotoxy 3 2
  133.   writestr op0 + (thousands line1):32  calc_display_color
  134.   gotoxy 3 3
  135.   writestr op1 + (thousands line2):32 calc_display_color
  136.   gotoxy 3 4
  137.   writestr op2 + (thousands line3):32 calc_display_color
  138.   gotoxy 3 5
  139.   writestr (if? op3 == '=' ' ' op3) + (thousands line4):32
  140.            calc_display_curr_color
  141. end
  142.  
  143. draw
  144.  
  145. event <destroy>
  146.   // call 'close' in object 'win'
  147.   close
  148. end
  149.  
  150. function "≡"
  151.   destroyobject
  152. end
  153.  
  154. // exit the calculator
  155. key <esc>
  156.   destroyobject
  157. end
  158.  
  159. // mouse click
  160. event <lbutton>
  161.  
  162.   // first pass on mouse events to the library
  163.   pass
  164.  
  165.   case getregion
  166.  
  167.     // client area
  168.     // translate mouse click to keypress
  169.     when 1
  170.       line = virtorow
  171.       buttonline = case line
  172.                      when 7  "cb%m/"
  173.                      when 9  "e789*"
  174.                      when 11 "r456-"
  175.                      when 13 "s123+"
  176.                      when 15 "z0ti="
  177.                      otherwise ''
  178.                    end
  179.  
  180.       if buttonline then
  181.         column = virtocol
  182.         bcol = if     column >= 3  and column <= 7  then 1
  183.                elseif column >= 10 and column <= 14 then 2
  184.                elseif column >= 17 and column <= 21 then 3
  185.                elseif column >= 24 and column <= 28 then 4
  186.                elseif column >= 31 and column <= 35 then 5
  187.                end
  188.  
  189.         if bcol then
  190.           buttonchar = buttonline [bcol]
  191.           hilite 5 1 (calc_button_hilite_color) (bcol * 7) - 4  line
  192.  
  193.           // simulate the appropriate keypress
  194.           if buttonchar then
  195.             queuekey (geteventcode '<'+ buttonchar + '>')
  196.           end
  197.         end
  198.       end
  199.   end
  200. end
  201.  
  202. event <lbuttonup>
  203.   if bcol then
  204.     hilite 5 1 (calc_button_color) (bcol * 7) - 4  line
  205.     bcol = ''
  206.   end
  207.   pass
  208. end
  209.  
  210. // clear the display
  211. key <del>
  212.   cleardisp
  213.  
  214. // enter into text
  215. key <ctrl enter>
  216.   send "≡"
  217.   queue "write" (thousands line4)
  218.  
  219. // paste from the clipboard to the calculator
  220. key <ctrl grey*>
  221.   oldmark = usemark _ClipName
  222.   number = sendobject "edit" "getmarktext"
  223.   usemark oldmark
  224.   if not posnot "0-9" number then
  225.     line4 = number
  226.     draw
  227.   end
  228.  
  229. // backspace
  230. key <backspace>
  231.   line4 = line4 / radix
  232.   op4 = ' '
  233.   op3 = ' '
  234.   draw
  235. end
  236.  
  237. key <enter>
  238.   if op3 <> ' ' then
  239.     if op3 == '=' then
  240.       op4 = op2
  241.       scrollup
  242.       line4 = line2
  243.     end
  244.     op4 = '='
  245.     result
  246.     draw
  247.   end
  248. end
  249.  
  250. // macro help
  251. macrofile = arg 1
  252. key <f1>
  253.   helpmacro macrofile
  254. end
  255.  
  256. // enter numeric characters
  257. key <char> (c)
  258.   if pos c "0123456789" then
  259.     if c < radix then
  260.       if op3 == '=' then
  261.         scrollup
  262.       end
  263.       positive = line4 >= 0
  264.       oldline = line4
  265.       line4 = line4 * radix + c
  266.       // test for overflow
  267.       if positive <> (line4 > 0) then
  268.         line4 = oldline
  269.       end
  270.       draw
  271.     end
  272.  
  273.     // operators
  274.   elseif pos c "+-*/%m" then
  275.     if op3 <> ' ' and op3 <> '=' then
  276.       op4 = '='
  277.       result
  278.     end
  279.     op4 = c
  280.     scrollup
  281.     draw
  282.  
  283.   elseif c == '=' then
  284.     send <enter>
  285.  
  286.   // do the current operation
  287.   else
  288.     case c
  289.  
  290.       when 'c'
  291.         cleardisp
  292.  
  293.       // clear the entry line
  294.       when 'e'
  295.         line4 = 0
  296.         op4 = ' '
  297.         op3 = ' '
  298.  
  299.       // store
  300.       when 's'
  301.         memory = line4
  302.  
  303.       // recall
  304.       when 'r'
  305.         line4 = memory
  306.  
  307.       // store + memory
  308.       when 'z'
  309.         memory = memory + line4
  310.  
  311.       // change sign
  312.       when 'i'
  313.         line4 = -line4
  314.         op4 = ' '
  315.         op3 = ' '
  316.  
  317.       // enter into text
  318.       when 't'
  319.         send <ctrl enter>
  320.         return
  321.  
  322.       // backspace
  323.       when 'b'
  324.         send <backspace>
  325.  
  326.       // unrecognized key
  327.       otherwise
  328.         send <otherkey>
  329.     end
  330.     draw
  331.   end
  332. end
  333.  
  334. // unrecognized key
  335. key <otherkey>
  336.   beep 200 70
  337. end
  338.